// component/information/index.js
const api = require('../../config/home.js');
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    informationCategory: {
      type: Object,
      value: {},
      observer: function (newVal) {
        // 属性值变化时执行
        if (newVal) {
          this.setData({
            titleName: newVal.dictLabel,
          })
        }
        this.getClassifyList(newVal)
      }
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    titleName: '',
    informationList: []
  },

  /**
   * 组件的方法列表
   */
  methods: {
    gomore(options) {
      wx.navigateTo({
        url: `/pages/information/informationList/index?classify=${this.data.informationCategory.dictValue}&classification=${options.currentTarget.dataset.value}`
      })
    },
    swipclick(e) {
      const {
        advertisementtype,
        id
      } = e.currentTarget.dataset
      wx.navigateTo({
        url: `/pages/infoDetail/index?advertisementtype=${advertisementtype}&id=${id}`
      });
    },
    getClassifyList(classify){
      let informationTypes = JSON.parse(this.data.informationCategory.remark || '[]') 
      if(informationTypes && informationTypes.length){
        this.data.informationList = []
        informationTypes.forEach(element => {
          if (element) {
            let information = {
              classification: element.value,
              classificationName: element.label,
              list: []
            }
            this.getInformationList(classify,information)
          }
        });
      }

    },
    getInformationList(classify,information) {
      if (classify && information && information.classification) {
        wx.request({
          url: api.informationUrl,
          data: {
            classify: classify.dictValue,
            classification:information.classification,
            pageNum: 1,
            pageSize: 6
          },
          method: 'GET',
          success: (res) => {
            if (res.data.code === 200) {
              let objs = res.data.information.records || []
              information.list = objs  || []
              if(information.list && information.list.length){
                this.data.informationList.push(information)
                this.setData({
                  informationList: this.data.informationList
                })
              }
              
            }

          }
        });
      }
    }
  }
})